#include "giffilterproxymodel.h"

GifFilterProxyModel::GifFilterProxyModel(QObject *parent)
    : QSortFilterProxyModel{parent}
    , m_filterEnabled(false)
{

}

bool GifFilterProxyModel::filterEnabled() const
{
    return m_filterEnabled;
}

void GifFilterProxyModel::setFilterEnabled(bool newFilterEnabled)
{
    if (m_filterEnabled == newFilterEnabled)
        return;
    m_filterEnabled = newFilterEnabled;
    emit filterEnabledChanged();

    invalidateFilter();
}

QStringList GifFilterProxyModel::disguisedGifs() const
{
    return m_disguisedGifs;
}

void GifFilterProxyModel::setDisguisedGifs(const QStringList &newDisguisedGifs)
{
    if (m_disguisedGifs == newDisguisedGifs)
        return;
    m_disguisedGifs = newDisguisedGifs;
    emit disguisedGifsChanged();

    invalidateFilter();
}

bool GifFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
    if (!m_filterEnabled) {
        return true;  // 如果过滤未启用，显示所有行
    }

    QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
    QString filePath = index.data().toString();

    // 条件1: 扩展名是 .gif
    bool isGifExtension = filePath.endsWith(".gif", Qt::CaseInsensitive);

    // 条件2: 文件在 gifs 列表中（伪装成 png/jpg 的 GIF）
    bool isDisguisedGif = m_disguisedGifs.contains(filePath);

    return isGifExtension || isDisguisedGif;
}
